<?php

/**
 * JingYao-backend
 *
 * @link     https://gitee.com/wang-zhihui-release/jingyao-backend
 * @apiDocument https://gitee.com/wang-zhihui-release/jingyao-backend/wikis/
 */

namespace App\Http\Controllers;

use App\Format\RecruitmentFormat;
use App\Http\Requests\CheckRequest;
use App\Http\Requests\SetRecruitmentForCheckRequst;
use App\Services\RecruitmentService;
use Illuminate\Http\Request;

class RecruitmentController extends Controller
{
    protected $recruitmentService;

    public function __construct(RecruitmentService $recruitmentService)
    {
        $this->recruitmentService = $recruitmentService;
    }

    public function setRecruitmentForCheck(SetRecruitmentForCheckRequst $setRecruitmentForCheck)
    {
        $format = new RecruitmentFormat($setRecruitmentForCheck->all());
        $this->recruitmentService->setRecruitmentForCheck($format, $setRecruitmentForCheck->input('user_id'));
        return renderSuccessJson();
    }

    public function getRecruitmentList(Request $request)
    {
        $page = $request->input('page', 1);
        $pageSize = $request->input('page_size', 10);
        $search = [];
        if ($request->input('job_name')) {
            $search['job_name'] = $request->input('job_name');
        }
        if ($request->input('start_date')) {
            $search['start_date'] = $request->input('start_date');
        }
        if ($request->input('end_date')) {
            $search['end_date'] = $request->input('end_date');
        }
        $res = $this->recruitmentService->getRecruitmentList($page, $pageSize, $search);
        return renderSuccessJson($res);
    }

    public function getAdminList(Request $request)
    {
        $page = $request->input('page', 1);
        $pageSize = $request->input('page_size', 10);
        $search = [];
        if ($request->input('job_name')) {
            $search['job_name'] = $request->input('job_name');
        }
        if ($request->input('start_date')) {
            $search['start_date'] = $request->input('start_date');
        }
        if ($request->input('end_date')) {
            $search['end_date'] = $request->input('end_date');
        }
        if ($request->input('check_status')) {
            $search['check_status'] = $request->input('check_status');
        }
        $res = $this->recruitmentService->getRecruitmentListForAdmin($page, $pageSize, $search);
        return renderSuccessJson($res);
    }

    public function checkRecruitment(CheckRequest $checkRequest)
    {
        $id = $checkRequest->input('id');
        $checkStatus = $checkRequest->input('check_status');
        $checkMessage = $checkRequest->input('check_message') ?? '';
        $this->recruitmentService->checkRecruitment($id, $checkStatus, $checkMessage);
        return renderSuccessJson();
    }

    public function delete(Request $request)
    {
        $id = $request->input('id');
        $this->recruitmentService->delete($id);
        return renderSuccessJson();
    }

    public function setExpireTime(Request $request)
    {
        $id = $request->input('id');
        $expireTime = $request->input('expire_time');
        $this->recruitmentService->setExpireTime($id, $expireTime);
        return renderSuccessJson();
    }

    public function detail(Request $request)
    {
        $id = $request->input('id');
        $res = $this->recruitmentService->detail($id);
        return renderSuccessJson($res);
    }

    public function detailAdmin(Request $request)
    {
        $id = $request->input('id');
        $res = $this->recruitmentService->detailAdmin($id);
        return renderSuccessJson($res);
    }

    public function getMyRecruitment(Request $request)
    {
        $page = $request->input('page', 1);
        $pageSize = $request->input('pageSize', 10);
        $userId = $request->input('user_id');
        $res = $this->recruitmentService->getMyRecruitment($page, $pageSize, $userId);
        return renderSuccessJson($res);
    }

    public function getMyRecruitmentDetail(Request $request)
    {
        $id = $request->input('id');
        $userId = $request->input('user_id');
        $res = $this->recruitmentService->getMyRecruitmentDetail($id, $userId);
        return renderSuccessJson($res);
    }
}
